package com.ssm.controller;




import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


import com.ssm.pojo.Person;
import com.ssm.pojo.Position;
import com.ssm.service.PersonService;
import com.ssm.util.DateUtil;



@Controller
@RequestMapping("/person")
public class PersonController {
	@Resource
	private PersonService personServiceImpl;
	private DateUtil date;
	

	@RequestMapping("/register")
	public String register(Person person,MultipartFile file,HttpServletRequest request) {
		
		String fileName = UUID.randomUUID().toString()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String path = request.getServletContext().getRealPath("images")+"/"+fileName;
		try {
			FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
		//只能取到webapps文件夹内容
		person.setPhoto(fileName);
		person.setPubtime(date.getcurrDate());
		int index = personServiceImpl.insRegister(person);
		if(index>0){
			
			return "redirect:/index.jsp";
		}else{
			return "redirect:/register.jsp";
		}
	}
	/**
	 * 登录
	 * @param session
	 * @param request
	 * @param person
	 * @return
	 */
	@RequestMapping("/login")
	public String login(HttpSession session, HttpServletRequest request, Person person){
		Person user = personServiceImpl.selLogin(person);
		if(user!=null){
			request.getSession().setAttribute("user", user);
			return "redirect:/index.jsp";
		}else{
			return "redirect:/login.jsp";
		}
		
		
		
		
		
	}
	/**
	 * 退出
	 * @return
	 */
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("user");
		return "redirect:/index.jsp";
	}
	
//	/**
//	 * 显示职位类表
//	 * @param session
//	 * @return
//	 */
//	@RequestMapping(value = "/showposition")
//	public String showposition(HttpSession session,HttpServletRequest req) {
//		List<Position> positions = personServiceImpl.selall();
//		req.setAttribute("positions", positions);
//		return "person/position";
//	}
	@RequestMapping(value="/reset")
	public String reset(Person person, MultipartFile file,HttpServletRequest request ,HttpSession session){
		if(file!=null){
			String fileName = UUID.randomUUID().toString()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			String path = request.getServletContext().getRealPath("images")+"/"+fileName;
			try {
				FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path));
			} catch (IOException e) {
				e.printStackTrace();
			}
			//只能取到webapps文件夹内容
			person.setPhoto(fileName);
			person.setPubtime(date.getcurrDate());
		}
		
		int index = personServiceImpl.updateSelective(person);
		if(index==1){
			request.getSession().setAttribute("setflag", "1");
			this.logout(session);
			return "redirect:/login.jsp";
		}else{
			request.getSession().setAttribute("setflag", "服务器繁忙，稍后在试！");
			return "redirect:/person/resetpassword.jsp";
		}	 
	}
	@RequestMapping(value="/resetinfo")
	public String resetinfo(Person person, MultipartFile file,HttpServletRequest request ,HttpSession session){
		if(file!=null){
			String fileName = UUID.randomUUID().toString()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			String path = request.getServletContext().getRealPath("images")+"/"+fileName;
			try {
				FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path));
			} catch (IOException e) {
				e.printStackTrace();
			}
			//只能取到webapps文件夹内容
			person.setPhoto(fileName);
			person.setPubtime(date.getcurrDate());
		}
		
		int index = personServiceImpl.updateSelective(person);
		if(index==1){
			Person user = personServiceImpl.selectbyid(person.getId());
			request.getSession().setAttribute("user", user);
			request.getSession().setAttribute("setflag1", "1");
		
		}else{
			request.getSession().setAttribute("setflag1", "0");
		}	 
		return "redirect:/person/myinfo.jsp";
	}
	
	
}

